<?php

	// create the config file
	$path = '..' . DIRECTORY_SEPARATOR . 'config.php';

	if(!$fh = fopen($path, 'w+')) 
	{
	    die('Can\'t create the "config.php" file.' . "\n" . 'Please make sure you have the right permissions');
    }

	// write the string to be set in the file
	$data = '<?php

	// create some constants

	// database
	define("DB_HOST", "' . $_POST['db_host'] . '");
	define("DB_USER", "' . $_POST['db_user'] . '");
	define("DB_PASS", "' . $_POST['db_pass'] . '");
	define("DB_NAME", "' . $_POST['db_name'] . '");
	define("DB_PREF", "' . $_POST['db_pref'] . '");

	// admin
	define("ADMIN_USER", "' . $_POST['admin_user'] . '");
	define("ADMIN_PASS", "' . $_POST['admin_pass'] . '");

?>';

	fwrite($fh, $data);
	fclose($fh);

	// now get our assets, all files should be set up
	require_once('..' . DIRECTORY_SEPARATOR . 'assets.php');
	
	// check if we have access
	if($db->error != '')
	{
	    unlink($path);
	    die($db->error);
	}

	$db->query("create table `" . $db->table('sites') . "` (
				`site_id` int unsigned not null primary key auto_increment,
				`site` varchar(256) not null,
				`url` varchar(256) not null,
				`key` varchar(16) not null,
				`ratio` tinyint unsigned not null
				)");
	$db->query("create table `" . $db->table('actions') . "` (
				`action_id` int unsigned not null primary key auto_increment,
				`action` text not null,
				`title` varchar(256) not null,
				`html` longtext
				)");
	$db->query("create table `" . $db->table('visits') . "` (
				`visit_id` int unsigned not null primary key auto_increment,
				`site_id` int unsigned not null,
				`session_id` varchar(32) not null,
				`view` varchar(16) not null,
				`size` varchar(16) not null,
				`browser_name` varchar(16) not null,
				`browser_version` varchar(8) not null,
				`browser_platform` varchar(16) not null,
				`entry_action_id` int unsigned not null,
				`referrer_action_id` int unsigned not null,
				`exit_action_id` int unsigned not null,
				`local_datetime` datetime not null,
				`server_datetime` datetime not null,
				`load_time` tinyint unsigned not null,
				`total_time` smallint unsigned not null,
				`events` longtext not null
				)");

	// set cookie
	$cookie->set('admin', 'true');

	// oh good, success
	die('Installation went well, the rest is done through the Control Panel');

?>